package manaul;

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class Rest1 {
	public static int rank(int key, int[] a)
	{ // 鏁扮粍蹇呴』鏄湁搴忕殑
	int lo = 0;
	int hi = a.length - 1;
	while (lo <= hi)
	{ // 琚煡鎵剧殑閿涔堜笉瀛樺湪锛岃涔堝繀鐒跺瓨鍦ㄤ簬a[lo..hi] 涔嬩腑
		if (a[lo]==a[hi]&&a[lo]==key) 
			return hi-lo+1;
		if (a[lo]!=key)
			lo=lo+1;
		if (a[hi]!=key) 
			hi=hi-1;
	}
	return 0 ;
	}
	public static int count(int key, int[] a)
	{ // 鏁扮粍蹇呴』鏄湁搴忕殑
	int lo = 0;
	int hi = a.length - 1;
	while (lo <= hi)
	{ // 琚煡鎵剧殑閿涔堜笉瀛樺湪锛岃涔堝繀鐒跺瓨鍦ㄤ簬a[lo..hi] 涔嬩腑
		if (a[lo]==a[hi]&&a[lo]==key) 
			return lo;
		if (a[lo]!=key)
			lo=lo+1;
		if (a[hi]!=key) 
			hi=hi-1;
	}
	return 0;
	}
	public static void main(String[] args) {
		int[] whitelist = { 1, 2,2, 3,3,4, 4, 6, 7, 8, 9, 12 };
		StdOut.print("杈撳叆涓�涓猭ey\n");
		int key=StdIn.readInt();
		//StdOut.println(key);
		StdOut.print("鍜宬ey鐩哥瓑鐨勬暟閲�"+rank(key, whitelist));
		StdOut.print("  灏忎簬key鐨勬暟閲�"+count(key, whitelist));
	}
}

